<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:vipRechargeOptions:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:vipRechargeOptions:edit']"
        >修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:vipRechargeOptions:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:vipRechargeOptions:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vipRechargeOptionsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="时长(月数)" align="center" prop="duration" />
      <el-table-column label="价格（单位元）" align="center" prop="price" />
      <el-table-column label="vip等级id" align="center" prop="vipName" />
      <el-table-column label="前端展示时间" align="center" prop="timeString" />
      <el-table-column label="折扣信息" align="center" prop="discountTip" />
      <el-table-column label="展示标签" align="center" prop="tag" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:vipRechargeOptions:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:vipRechargeOptions:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充值选项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="时长(月数)" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入时长(天数)" />
        </el-form-item>
        <el-form-item label="价格(元)" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>

        <el-form-item label="前端展示时间" prop="timeString">
          <el-input v-model="form.timeString" placeholder="请输入前端展示时间" />
        </el-form-item>
        <el-form-item label="折扣信息" prop="discountTip">
          <el-input v-model="form.discountTip" placeholder="请输入折扣信息" />
        </el-form-item>
        <el-form-item label="展示标签" prop="tag">
          <el-input v-model="form.tag" placeholder="请输入展示标签" />
        </el-form-item>
        <el-form-item label="vip等级id">
          <el-select v-model="form.vipLevel" placeholder="请选择会员等级">
            <el-option
              v-for="item in vipLevelList"
              :key="item.id"
              :label="item.levelName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVipRechargeOptions, getVipRechargeOptions, delVipRechargeOptions, addVipRechargeOptions, updateVipRechargeOptions } from "@/api/system/vipRechargeOptions";
import { listVipLevels } from "@/api/system/vipLevels";
export default {
  name: "VipRechargeOptions",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值选项表格数据
      vipRechargeOptionsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        duration: null,
        price: null,
        vipLevel: null,
        timeString: null,
        discountTip: null,
        tag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 岗位选项
      vipLevelList: [],
      vipLevelQueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询充值选项列表 */
    getList() {
      this.loading = true;
      listVipRechargeOptions(this.queryParams).then(response => {
        this.vipRechargeOptionsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        duration: null,
        price: null,
        vipLevel: null,
        createTime: null,
        timeString: null,
        discountTip: null,
        tag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();

      listVipLevels(this.vipLevelQueryParams).then(response => {
        this.vipLevelList = response.rows;
        this.open = true;
        this.title = "添加充值选项";
      });


    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      listVipLevels(this.vipLevelQueryParams).then(response => {
        this.vipLevelList = response.rows;
        getVipRechargeOptions(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改充值选项";
        });
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVipRechargeOptions(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVipRechargeOptions(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除充值选项编号为"' + ids + '"的数据项？').then(function() {
        return delVipRechargeOptions(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/vipRechargeOptions/export', {
        ...this.queryParams
      }, `vipRechargeOptions_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
